package com.sharekitchen.service.impl;


import com.sharekitchen.dto.RoomDevDto;
import com.sharekitchen.dto.RoomDto;
import com.sharekitchen.entity.*;
import com.sharekitchen.mapper.DeviceMapper;
import com.sharekitchen.mapper.RoomMapper;
import com.sharekitchen.service.RoomService;
import com.sharekitchen.utils.gaodeAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Room> findAllRoom() {
        return roomMapper.findAllRoom();
    }

    @Override
    public List<RoomDevice> findAllRoomDev() {
        return roomMapper.findAllRoomDev();
    }

    @Override
    public List<RoomImages> findAllRoomImages() {
        return roomMapper.findAllRoomImages();
    }

    @Override
    public List<RoomAddress> findAllRoomAddr() {
        return roomMapper.findAllRoomAddr();
    }

    @Override
    public Room findRoomById(String roomId) {
        return roomMapper.findRoomById(roomId);
    }

    @Override
    public RoomImages findRoomImagesById(String roomId) {
        return roomMapper.findRoomImagesById(roomId);
    }

    @Override
    public RoomAddress findRoomAddrById(String roomId) {
        return roomMapper.findRoomAddrById(roomId);
    }

    @Override
    public List<RoomDevice> findRoomDevById(String roomId) {
        return roomMapper.findRoomDevById(roomId);
    }

    @Override
    public boolean addRoom(RoomDevDto roomDevDto) {
        RoomDto roomDto = new RoomDto();
        String newRoomId = UUID.randomUUID().toString().replace("-", "").substring(0, 20);
        roomDto.setRoomId(newRoomId);
        roomDto.setRoomStatus(0);
        roomDto.setStar(10);
        ArrayList<RoomDevice> roomDevs = new ArrayList<>();
        List<String> devIds = roomDevDto.getDevIds();
        for (String s : devIds) {
            RoomDevice roomDevice = new RoomDevice();
            roomDevice.setDevId(s);
            roomDevice.setRoomId(newRoomId);
            roomDevs.add(roomDevice);
        }
        roomDto.setRoomDevices(roomDevs);
        String address = roomDevDto.getProvince() + roomDevDto.getCity() + roomDevDto.getDistrict() + roomDevDto.getDetail();
        String list = gaodeAPI.get(address);
        RoomAddress roomAddress = new RoomAddress();
        roomAddress.setRoomId(newRoomId);
        roomAddress.setProvince(roomDevDto.getProvince());
        roomAddress.setCity(roomDevDto.getCity());
        roomAddress.setDistrict(roomDevDto.getDistrict());
        roomAddress.setDetail(roomDevDto.getDetail());
        roomAddress.setLongitude(list.split(",")[0]);
        roomAddress.setLatitude(list.split(",")[1]);
        roomDto.setRoomAddress(roomAddress);
        String url = "https://images.unsplash.com/photo-1556912173-3bb406ef7e77?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NHx8a2l0Y2hlbiUyMGJhY2tncm91bmR8ZW58MHx8MHx8fDA%3D";
        roomDto.setRoomImages(new RoomImages(0,url,newRoomId,0));
        if (roomMapper.addRoom(roomDto) <= 0) {
            return false;
        }
        for (RoomDevice roomDevice :roomDto.getRoomDevices()) {
            if (roomMapper.addRoomDevice(roomDevice) <= 0) {
                return false;
            }
        }
        if (roomMapper.addRoomAddr(roomDto.getRoomAddress()) <= 0) {
            return false;
        }
        if (roomMapper.addRoomImages(roomDto.getRoomImages()) <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean delRoom(String roomId) {
        if (roomMapper.delRoom(roomId) <= 0) {
            return false;
        }
        if (roomMapper.delRoomAddr(roomId) <= 0) {
            return false;
        }
        if (roomMapper.delRoomDevice(roomId) <= 0) {
            return false;
        }
        if (roomMapper.delRoomImages(roomId) <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean updateRoom(RoomDto roomDto) {
        if (roomMapper.updateRoom(roomDto) <= 0) {
            return false;
        }

        if (roomMapper.updateRoomAddr(roomDto.getRoomAddress()) <= 0) {
            return false;
        }

        for (RoomDevice roomDevice :roomDto.getRoomDevices()) {
            if (roomMapper.updateRoomDevice(roomDevice) <= 0) {
                return false;
            }
        }


        if (roomMapper.updateRoomImages(roomDto.getRoomImages()) <= 0) {
            return false;
        }

        return true;
    }

    @Override
    public boolean addRoomDev(String roomId, String devId) {
        return addRoomDev(roomId,devId);
    }
}
